/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 string_insensitive_compare.cpp

	$Header: /heroes4/string_insensitive_compare.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "string_insensitive_compare.h"

// -------------------------------------------------------------------------------
// Unnamed namespace
// -------------------------------------------------------------------------------

namespace
{

	// -------------------------------------------------------------------------------
	// do_compare() function
	// -------------------------------------------------------------------------------

	inline int do_compare( char const * left_data, size_t left_len, char const * right_data, size_t right_len )
	{
		size_t common_len = left_len < right_len ? left_len : right_len;
		int result = _memicmp( left_data, right_data, common_len );
		if ( result != 0 )
			return result;
		return int( left_len ) - int( right_len );
	}

} // Unnamed namespace

// -------------------------------------------------------------------------------
// string_insensitive_compare() function - does a case insensitive compare of
// two std::string's.
// -------------------------------------------------------------------------------

int string_insensitive_compare( std::string const & left, std::string const & right )
{
	return do_compare( left.data(), left.size(), right.data(), right.size() );
}

// -------------------------------------------------------------------------------
// string_insensitive_compare() function - does a case insensitive compare of
// a std::string with a c-style string.
// -------------------------------------------------------------------------------

int string_insensitive_compare( std::string const & left, char const * right )
{
	return do_compare( left.data(), left.size(), right, strlen( right ) );
}
